package com.cskaoyan.market.service.wx.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.db.mapper.MarketCouponUserMapper;
import com.cskaoyan.market.service.wx.WxCouponService;
import com.cskaoyan.market.vo.wx.lhh.CouponExchangeVo;
import com.cskaoyan.market.vo.wx.lyt.CouponListVo;

import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: WxCouponService
 * @Auther: liz
 * @Date: 2023/05/22/14:32
 * @Description:
 */
@Service
public class WxCouponServiceImpl implements WxCouponService {

    @Autowired
    MarketCouponUserMapper couponUserMapper;

    @Autowired
    MarketCouponMapper couponMapper;

    @Override
    public List<CouponListVo> list() {
        List<CouponListVo> couponList = new ArrayList<>();
        MarketCouponExample couponExample = new MarketCouponExample();
        couponExample.or().andDeletedEqualTo(false);
        List<MarketCoupon> coupons = couponMapper.selectByExample(couponExample);
        for (MarketCoupon coupon : coupons) {
            CouponListVo couponListVo = new CouponListVo();
            couponListVo.setId(coupon.getId());
            couponListVo.setName(coupon.getName());
            couponListVo.setDesc(coupon.getDesc());
            couponListVo.setTag(coupon.getTag());
            couponListVo.setDiscount(coupon.getDiscount());
            couponListVo.setMin(coupon.getMin());
            couponListVo.setDays(coupon.getDays());

            couponList.add(couponListVo);
        }
        return couponList;
    }


    @Override
    public Integer receiveCoupon(Integer couponId) {

        MarketCoupon marketCoupon = couponMapper.selectByPrimaryKey(couponId);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer userId = (Integer) session.getAttribute("userId");
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        marketCouponUserExample.or().andDeletedEqualTo(false).andCouponIdEqualTo(couponId).andUserIdEqualTo(userId);
        //拿到用户拥有的优惠券
        List<MarketCouponUser> coupons = couponUserMapper.selectByExample(marketCouponUserExample);
        //不等于0说明用户已经有这个优惠券了;
        if (coupons.size() != 0) {
            return 1;
        }
        //数量小于0说明优惠券不够了
        if (marketCoupon.getTotal() <= 0) {
            return 2;
        }

        MarketCouponUser couponUser = new MarketCouponUser();
        couponUser.setUserId(userId);
        couponUser.setCouponId(couponId);
        couponUser.setStatus((short) 0);
        couponUser.setStartTime(marketCoupon.getStartTime());
        couponUser.setEndTime(marketCoupon.getEndTime());
        couponUser.setAddTime(LocalDateTime.now());
        couponUser.setUpdateTime(LocalDateTime.now());
        couponUser.setDeleted(false);

        couponUserMapper.insertSelective(couponUser);
        //优惠券减一
        marketCoupon.setTotal(marketCoupon.getTotal() - 1);
        couponMapper.updateByPrimaryKey(marketCoupon);
        return 0;
    }

    @Override
    public Integer exchange(String code, Integer userId) {

        MarketCouponExample marketCouponExample = new MarketCouponExample();
        marketCouponExample.or().andCodeEqualTo(code);
        MarketCoupon marketCoupon = couponMapper.selectOneByExample(marketCouponExample);
if (marketCoupon==null){
    return null;
}
        Integer couponId = marketCoupon.getId();

        return couponId;
    }


    @Override
    public List<CouponListVo> mylist(Short status, Integer page, Integer limit, Integer userId) {

        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        marketCouponUserExample.or().andDeletedEqualTo(false).andUserIdEqualTo(userId).andStatusEqualTo(status);
        PageHelper.startPage(page, limit);
        List<MarketCouponUser> couponUsers = couponUserMapper.selectByExample(marketCouponUserExample);
        List<CouponListVo> result = new ArrayList<>();

        for (MarketCouponUser couponUser : couponUsers) {

            CouponListVo couponListVo = new CouponListVo();
            Integer couponId = couponUser.getCouponId();
            MarketCoupon marketCoupon = couponMapper.selectByPrimaryKey(couponId);
            couponListVo.setId(marketCoupon.getId());
            couponListVo.setName(marketCoupon.getName());
            couponListVo.setDesc(marketCoupon.getDesc());
            couponListVo.setTag(marketCoupon.getTag());
            couponListVo.setDiscount(marketCoupon.getDiscount());
            couponListVo.setMin(marketCoupon.getMin());
            couponListVo.setDays(marketCoupon.getDays());
            couponListVo.setStartTime(marketCoupon.getStartTime());
            couponListVo.setEndTime(marketCoupon.getEndTime());

            result.add(couponListVo);

        }
        return result;

    }

    @Override
    public List<CouponExchangeVo> selectList(Integer cartId, Integer grouponRulesId, Integer userId) {
        MarketCouponUserExample couponUserExample = new MarketCouponUserExample();
        couponUserExample.or().andDeletedEqualTo(false).andUserIdEqualTo(userId);

        List<MarketCouponUser> couponUserList = couponUserMapper.selectByExample(couponUserExample);
        List<CouponExchangeVo> result = new ArrayList<>();
        for (MarketCouponUser couponUser : couponUserList) {
            CouponExchangeVo couponVo = new CouponExchangeVo();
            couponVo.setId(couponUser.getId());
            Integer couponId = couponUser.getCouponId();
            couponVo.setCid(couponId);
            // 查询对应的优惠券
            MarketCoupon coupon = couponMapper.selectByPrimaryKey(couponId);
            couponVo.setName(coupon.getName());
            couponVo.setDesc(coupon.getDesc());
            couponVo.setTag(coupon.getTag());
            couponVo.setMin(coupon.getMin());
            couponVo.setDiscount(coupon.getDiscount());
            couponVo.setStartTime(coupon.getStartTime());
            couponVo.setEndTime(coupon.getEndTime());
            Short status = couponUser.getStatus();
            if (status == 0) {
                couponVo.setAvailable(true);
            }else {
                couponVo.setAvailable(false);
            }
            result.add(couponVo);
        }

        return result;
    }
}
